<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入你要评论的内容(最多输入120个字)" maxlength="120" v-model="msg"></textarea>

        <mt-button type="primary" size="large" @click="postComment()">发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="item in commentList" >
                <div class="cmt-title">用户：{{item.username}}  发表时间：{{item.add_time | dateFormat()}}</div>
                <div class="cmt-body">
                    {{item.content}}
                </div>
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getMore()">加载更多</mt-button>
    </div>
</template>

<script>
    import {Toast} from 'mint-ui'
    export default {
        name: "comment",
        data(){
            return {
                msg:"",//评论输入的内容
                commentList:[]
            }
        },
        props:['parentId'],
        created(){
            this.getCommentList()
        },
        methods:{
            getCommentList(){
                this.$http.get('http://192.168.1.237:9090/test33/test.php').then(result=>{
                    if(result.body.code==0){
                        result.body.newslist.forEach(item=>{
                            if (item.id==this.parentId){
                                //每当获取新评论数据的时候，不要把老数据清空覆盖，而是应该以老数据拼接上新数据
                                this.commentList=this.commentList.concat(item.comment)
                            }
                        })
                    }
                })
            },
            getMore(){
                this.getCommentList()
            },
           // trim()去掉字符串前后空格
            postComment(){
                if (this.msg.trim().length===0){
                   return Toast("评论内容不能为空")
                }
                // 提交评论内容至服务器
               /* this.$http.post("url",{
                    content:this.msg.trim()
                }).then(function () {
                    
                })*/
                this.commentList.unshift({commenId:11,username:'匿名用户333',add_time:new Date(),content:this.msg})
                this.msg=""
            }
        }
    }
</script>

<style scoped lang="scss">
    .cmt-container {
        h3{
            font-size: 16px;
        }
        textarea{
            font-size: 14px;
            height: 85px;
            margin: 0;
        }
        .cmt-list{
            margin: 5px 0;
            .cmt-item{
                font-size: 13px;
                .cmt-title{
                    line-height: 30px;
                    background-color: #ccc;
                }
                .cmt-body{
                    line-height: 35px;
                    text-indent: 2em;
                }
            }
        }

    }

</style>